package com.huifaapp.mall.activity.Homefragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.LoginActivity;
import com.huifaapp.mall.adapter.MessageAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.bean.MeaageBean;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

public class MessageActivity extends AppCompatActivity {



    private RecyclerView recyclerView;
    private MessageAdapter messageAdapter;
    private List<MeaageBean.DataBean>list;
    private ImageView fanhui;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message2);
        initView();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.message_recy);
fanhui = (ImageView) findViewById(R.id.messagegege_back);
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        OkHttpUtils.get().url("http://develop.jhjvip.cn:8080/mall/mobile/msg/type")
                .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                .addParams("appId", AUniqueIdentifier.getDeviceId(MessageActivity.this))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            Gson gson = new Gson();
                            MeaageBean meaageBean = gson.fromJson(response, MeaageBean.class);
                            if (meaageBean.getCode().equals("10000")) {
                                list = meaageBean.getData();
                                messageAdapter = new MessageAdapter(MessageActivity.this, list);
                                recyclerView.setAdapter(messageAdapter);
                                messageAdapter.notifyDataSetChanged();
                            }else if(meaageBean.getCode().equals("10001")){
                                Intent intent=new Intent(MessageActivity.this, LoginActivity.class);
                                startActivity(intent);
                            }else{
                                Toast.makeText(MessageActivity.this,meaageBean.getMsg(),Toast.LENGTH_SHORT).show();
                            }

                        } catch(Exception e){
                                e.printStackTrace();
                            }

                    }
                });
    }
}